package meijuli.com.join51.business.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.BankMsg;
import meijuli.com.join51.business.ui.widget.PwdEditText;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

/**
 * 支付密码弹窗
 *
 * @author jack 2017-05-08 11:35
 */
public class PayPwdDialog extends Dialog {
    private PwdEditText mPwdEditText;
    private OnListener mOnListener;
    private TextView mForgetText;

    public PayPwdDialog(@NonNull final Context context, BankMsg bankMsg) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = View.inflate(context, R.layout.dialog_paypwd, null);
        setContentView(contentView);
        mPwdEditText = (PwdEditText) findViewById(R.id.pwdEditText);
        mForgetText = (TextView) findViewById(R.id.text_forget);
        TextView titleText = (TextView) findViewById(R.id.text_title);
        setBankMsg(bankMsg);
        titleText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard(mPwdEditText);
                mPwdEditText.setText("");
                dismiss();
            }
        });
        mPwdEditText.setOnInputListener(new PwdEditText.OnInputListener() {
            @Override
            public void onInput(CharSequence text) {

            }

            @Override
            public void onInputFinish(CharSequence text) {
                if (mOnListener != null) {
                    mOnListener.onInputFinish(text);
                }
                hideKeyboard(mPwdEditText);
                mPwdEditText.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPwdEditText.setText("");
                        dismiss();
                    }
                }, 300);
            }
        });
        String text = mForgetText.getText().toString();
        if (!TextUtils.isEmpty(text)) {
            SpannableString ss = new SpannableString(text);
            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff2d4b")),
                    text.indexOf("，") + 1,
                    text.length(),
                    SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ClickableSpan() {
                           @Override
                           public void onClick(View view) {
                               dismiss();
                               if (mOnListener != null) {
                                   mOnListener.onForgetClick(view);
                               }
                           }

                           @Override
                           public void updateDrawState(TextPaint ds) {
                               super.updateDrawState(ds);
                               ds.setUnderlineText(false);
                           }
                       },
                    text.indexOf("，") + 1,
                    text.length(),
                    SPAN_EXCLUSIVE_EXCLUSIVE);
            mForgetText.setText(ss);
            mForgetText.setHighlightColor(ContextCompat.getColor(context, R.color.transparent));
            mForgetText.setMovementMethod(LinkMovementMethod.getInstance());
        }

        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                mPwdEditText.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showKeyboard(mPwdEditText);
                    }
                }, 300);
            }
        });
    }

    private void setBankMsg(BankMsg bankMsg) {
        if (bankMsg != null) {
            String bankCardNum = bankMsg.getBankCardNum();
            mForgetText.setText(bankMsg.getBankName() +
                    "储蓄卡(" + bankCardNum.substring(bankCardNum.length() - 4, bankCardNum.length()) + ")" +
                    "，忘记密码？");
        }
    }

    private void showKeyboard(EditText editText) {
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    }

    private void hideKeyboard(EditText editText) {
        editText.clearFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    public void setOnListener(OnListener onListener) {
        mOnListener = onListener;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mPwdEditText != null) {
            mPwdEditText.setText("");
        }
    }

    public interface OnListener {
        void onInputFinish(CharSequence text);

        void onForgetClick(View view);
    }
}
